package demo1228.pm;

import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

public class SaleTicket2 implements Runnable {
    int tickets = 1000;
    //创建一个锁对象
    Lock lock = new ReentrantLock();
    @Override
    public void run() {
        while (tickets>0){
            //获取锁
            lock.lock();
            try {
                if (tickets > 0) {
                    System.out.println(Thread.currentThread().getName() +
                            "正在售卖第" + tickets + "张票,还有" + --tickets + "张票");
                }
            }finally {
                //释放锁
                lock.unlock();


            }
        }
    }
}
